<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-card">
        <div class="title-container">
          <h3 class="title">学术活动及科研成果管理系统</h3>
        </div>

        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
          label-position="left">
          <el-form-item prop="account">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input ref="username" v-model="loginForm.account" placeholder="请输入账号" name="username" type="text"
              tabindex="1" auto-complete="on" />
          </el-form-item>

          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType"
              placeholder="请输入密码" name="password" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin" />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>

          <el-button :loading="loading" type="primary" class="login-button"
            @click.native.prevent="handleLogin">登录</el-button>

          <div class="register-link">
            <span>还没有账号？</span>
            <router-link to="/register">立即注册</router-link>
          </div>
        </el-form>
      </div>
    </div>

    <div class="copyright">
      <span>@copyright 2003-2025 张子涵 陈兴瑶 期末作业版权所有</span>
    </div>
  </div>
</template>

<script>
import { validUsername } from "@/utils/validate";

export default {
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error("请输入正确的账号"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6 || value.length > 15) {
        callback(new Error("密码长度为6-15位"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        account: "admin",
        password: "123456",
      },
      loginRules: {
        account: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  methods: {
    // 是否显示密码
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },

    // 登录
    handleLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          try {
            let result = await this.$store.dispatch(
              "user/login",
              this.loginForm
            );
            this.$router.push({ path: this.redirect || "/dashboard" });
            this.$message({
              type: "success",
              duration: 2000,
              message: "登录成功",
            });
          } catch (error) {
          } finally {
            this.loading = false;
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
$cursor: #409EFF;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);

  .el-tabs__item {
    color: #606266;
    font-size: 16px;
    font-weight: 500;

    &.is-active {
      color: #409EFF;
    }
  }

  .el-tabs__nav-wrap::after {
    background-color: transparent;
  }

  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #333;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        transition: background-color 5000s ease-in-out 0s;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(0, 0, 0, 0.1);
    background: rgba(255, 255, 255, 0.9);
    border-radius: 5px;
    margin-bottom: 20px;
  }
}
</style>

<style lang="scss" scoped>
$dark_gray: #889aa4;
$light_gray: #333;

.login-container {
  min-height: 100%;
  width: 100%;
  overflow: hidden;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;

  .login-box {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100vh;
  }

  .login-card {
    width: 520px;
    max-height: 600px;
    padding: 30px;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 10px;
    box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
    backdrop-filter: blur(10px);
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }

  .login-form {
    width: 100%;
    max-width: 100%;
    margin: 0 auto;
    overflow: hidden;
    flex: 1;
    display: flex;
    flex-direction: column;
  }

  .login-button {
    width: 100%;
    margin-bottom: 20px;
    border-radius: 25px;
    height: 45px;
    font-size: 16px;
  }

  .register-link {
    text-align: center;
    margin-top: 10px;
    font-size: 14px;
    color: #606266;

    a {
      color: #409EFF;
      cursor: pointer;
      margin-left: 5px;
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 30px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .copyright {
    position: absolute;
    bottom: 20px;
    width: 100%;
    font-size: 14px;
    text-align: center;
    color: rgba(255, 255, 255, 0.8);
  }
}
</style>
